package com.lanyam.travel.base.aspect;

import com.lanyam.travel.base.entity.base.BaseResponse;
import com.lanyam.travel.base.entity.base.ErrorResponseBuilder;
import com.lanyam.travel.base.exception.ErrorParametersException;
import com.lanyam.travel.base.utils.LogUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author KingTang
 * @date   2019/12/20
 */
@ControllerAdvice
@Component
public abstract class BaseElinkcareController {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse handleException(Exception e){
        LogUtils.logger.debug("catch exception", e);
        if (e instanceof MissingServletRequestParameterException){
            e = new ErrorParametersException(e.getMessage());
        }
        return new ErrorResponseBuilder().exception(e).build();
    }
}